package com.campus.dao.impl;

import com.campus.dao.TeachingStudentLearningDao;
import com.campus.model.TeachingStudentLearning;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class TeachingStudentLearningDaoImpl implements TeachingStudentLearningDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public boolean createTeachingStudentLearning(TeachingStudentLearning studentLearning) {
        String sql = "INSERT INTO t_teaching_student_learning (stu_in_class_id, unit_id, finished_status, finished_time) " +
                "VALUES (?, ?, ?, ?)";
        return jdbcTemplate.update(sql, studentLearning.getStuInClassId(), studentLearning.getUnitId(),
                studentLearning.getFinishedStatus(), studentLearning.getFinishedTime()) > 0;
    }

    @Override
    public boolean updateTeachingStudentLearning(TeachingStudentLearning studentLearning) {
        String sql = "UPDATE t_teaching_student_learning SET finished_status = ?, finished_time = ? WHERE id = ?";
        return jdbcTemplate.update(sql, studentLearning.getFinishedStatus(), studentLearning.getFinishedTime(), studentLearning.getId()) > 0;
    }

    @Override
    public boolean deleteTeachingStudentLearning(int id) {
        String sql = "DELETE FROM t_teaching_student_learning WHERE id = ?";
        return jdbcTemplate.update(sql, id) > 0;
    }

    @Override
    public TeachingStudentLearning findById(int id) {
        String sql = "SELECT * FROM t_teaching_student_learning WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(TeachingStudentLearning.class), id);
    }

    @Override
    public List<TeachingStudentLearning> findAllByUnitId(int unitId) {
        String sql = "SELECT * FROM t_teaching_student_learning WHERE unit_id = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TeachingStudentLearning.class), unitId);
    }

    @Override
    public List<TeachingStudentLearning> findAll() {
        String sql = "SELECT * FROM t_teaching_student_learning";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TeachingStudentLearning.class));
    }
}
